<template>
  <div class="page">
    <el-form
      :model="addForm"
      ref="addForm"
      :rules="addFormRules"
      label-width="120px"
    >
      <el-form-item label="原密码：" prop="oldPassword">
        <el-input
          type="password"
          v-model="addForm.oldPassword"
          class="a_input"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="newPassword">
        <el-input
          type="password"
          v-model="addForm.newPassword"
          class="a_input"
        ></el-input>
      </el-form-item>
      <el-form-item label="重复新密码：" prop="newSecondPassword">
        <el-input
          type="password"
          v-model="addForm.newSecondPassword"
          class="a_input"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="saveInfo">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { USERCHANGEPASSWORD } from "@/http/system";
import { showToast } from "@/common/util";
export default {
  name: "UpdatePassword",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value !== this.addForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      addForm: {},
      addFormRules: {
        oldPassword: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        newSecondPassword: [
          { required: true, message: "请输入重复新密码", trigger: "blur" },
          { required: true, message: "请输入重复新密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
      },
      userInfo: {},
    };
  },
  mounted() {
    let m = sessionStorage.getItem("user");
    let o = m ? JSON.parse(m) : {};
    console.log(o);
    this.userInfo = o;
  },
  methods: {
    // 修改密码
    saveInfo() {
      let _this = this;
      _this.loading = true;
      _this.$refs.addForm.validate((valid) => {
        if (!valid) {
          _this.loading = false;
          return false;
        }
        let oParams = {};
        oParams.userId = this.userInfo.id;
        oParams.oldPassword = this.addForm.oldPassword;
        oParams.newPassword = this.addForm.newPassword;
        USERCHANGEPASSWORD(oParams)
          .then((res) => {
            _this.loading = false;
            if (res.code == 200) {
              showToast({
                message: "修改成功",
                type: "success",
              });
              _this.addForm = {};
              _this.$emit("change");
            }
          })
          .catch((error) => {
            _this.loading = false;
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.a_input {
  width: 240px;
}
.page {
  background: #fff;
  padding: 16px;
  height: 100%;
}
</style>